<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset" :inline="true">
      <template v-slot:body>
        <el-form-item label="其他入库单号" prop="othernumbers" maxlength="5">
          <el-input v-model="query.othernumbers" name="othernumbers" clearable />
        </el-form-item>
        <el-form-item label="入库时间" prop="incomingdate" maxlength="5">
          <el-date-picker v-model="query.incomingdate" value-format="YYYY-MM-DD" type="daterange" range-separator="到" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item label="入库类型" prop="incomingtype">
          <el-select v-model="query.incomingtype" placeholder="请选择入库类型" clearable>
            <el-option :label="item.label" :value="item.value" v-for="item in receiptType" :key="item.value" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="query.remark" name="remark" clearable />
        </el-form-item> -->
        <!-- <el-form-item label="状态" prop="state">
          <el-select v-model="query.state" placeholder="请选择状态" clearable>
            <el-option label="未审核" value="'未审核'" />
            <el-option label="已审核" value="'已审核'" />
          </el-select>
        </el-form-item> -->

        <!-- <el-form-item label="回写状态" prop="backstate">
          <el-select v-model="query.backstate" placeholder="请选择" clearable>
            <el-option label="未回写" value="0" />
            <el-option label="回写成功" value="1" />
            <el-option label="回写失败" value="2" />
          </el-select>
        </el-form-item> -->
      </template>
    </Search>
    <div class="table-default">
      <el-table :data="tableData" class="mt-3" v-loading="loading" border>
        <el-table-column prop="othernumbers" label="其他入库单号" width="200" />
        <el-table-column prop="incomingdate" label="入库时间" width="170" />
        <el-table-column prop="incomingtype" label="入库类型" width="120">
          <template #default="scope">
            <el-tag class="ml-2" type="primary" v-if="scope.row.incomingtype != ''">{{ scope.row.incomingtype }}</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="state" label="状态" width="100">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.state == '未审核'">{{ scope.row.state }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.state == '已审核'">{{ scope.row.state }}</el-tag>
            <el-tag class="ml-2" type="info" v-if="scope.row.state == '' || scope.row.state == null">无数据</el-tag>
          </template>
        </el-table-column> -->

        <el-table-column prop="writestate" label="回写状态" width="110">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.writestate == '回写失败'">{{ scope.row.writestate }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.writestate == '回写成功'">{{ scope.row.writestate }}</el-tag>
            <el-tag class="ml-2" type="info" v-if="scope.row.writestate == '' || scope.row.writestate == null">无数据</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="150" />
        <el-table-column prop="writeback" label="回写备注" width="110" />
        <el-table-column prop="creator_id" label="创建者" width="130" />
        <el-table-column prop="created_at" label="创建时间" width="150" />
        <el-table-column label="操作" fixed="right" width="130">
          <template #default="scope">
            <!-- {{JSON.stringify(scope.row)}} -->
            <div class="btn-box">
              <router-link :to="{ path: '/Inventory/warehousingDetail', query: { id: `${scope.row.id}` } }">
                <Show />
              </router-link>
              <!-- <el-button type="warning" size="small" @click="approveHandler(scope.row.id)" v-if="scope.row.state != '已审核'" v-loading.fullscreen.lock="fullscreenLoading"
                ><Icon name="wrench-screwdriver" />审核</el-button
              > -->
              <Destroy @click="destroy(api, scope.row.id)" style="margin-left: 0" v-if="scope.row.state != '已审核'" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>
    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, toRaw, ref, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import storage from '/admin/utils/storage'
import { setTableHeight } from '/admin/utils/setHeight.js'

let router = useRouter()

const api = 'intelligent/intelligent'

const { data, query, search, reset, loading, toraw } = useGetList(api)
const { destroy, deleted, delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

console.log(tableData)

const writestateFormatter = {
  0: '回写失败',
  1: '回写成功',
}
const fullscreenLoading = ref(false)

const approveHandler = id => {
  fullscreenLoading.value = true
  http
    .post(`intelligent/Auditinterface/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}
let receiptType = ref([])

const getType = () => {
  http.get(`intelligent/type`).then(r => {
    let res = r.data.data
    console.log('入库类型：', res.data)
    receiptType.value = res.data
    storage.setItem('receiptType', res.data)
  })
}

getType()

onMounted(() => {
  console.log('触发了onmounted')
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>
<style>
.btn-box {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.el-table {
  height: 87%;
}
</style>
